package com.Imp;

import com.entity.Cuisine;
import com.mapper.CuisineMapper;
import com.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CuisineMapperImpl implements CuisineMapper {

    public List<Cuisine> getCuisinesByStoreId(String storeId) throws IOException {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            return sqlSession.selectList("com.mapper.CuisineMapper.getCuisinesByStoreId", storeId);
        }
    }

    public Cuisine getCuisineByCuisineId(String cuisineId){
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            Map<String, Object> param = new HashMap<>();
            param.put("cuisineId", cuisineId);
            return sqlSession.selectOne("com.mapper.CuisineMapper.getCuisineByCuisineId", param);
        }
    }

    public void updateCuisine(Cuisine cuisine){
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            sqlSession.update("com.mapper.CuisineMapper.updateCuisine",cuisine);
            sqlSession.commit();  // 提交事务
        }
    }

    public void addCuisine(Cuisine cuisine){
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            sqlSession.insert("com.mapper.CuisineMapper.addCuisine",cuisine);
            sqlSession.commit();  // 提交事务
        }
    }

    public void deleteCuisine(String cuisineId){
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            sqlSession.delete("com.mapper.CuisineMapper.deleteCuisine",cuisineId);
            sqlSession.commit();  // 提交事务
        }
    }

    public List<Cuisine> getCartByStoreId(String storeId){
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            Map<String, Object> param = new HashMap<>();
            param.put("storeId", storeId);
            return sqlSession.selectList("com.mapper.CuisineMapper.getCartByStoreId", param);
        }
    }

    public void addCartCuisine(Cuisine cuisine){
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            sqlSession.insert("com.mapper.CuisineMapper.addCartCuisine",cuisine);
            sqlSession.commit();  // 提交事务
        }
    }



    public void deleteCartCuisine(String cuisineId){
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            sqlSession.delete("com.mapper.CuisineMapper.deleteCartCuisine",cuisineId);
            sqlSession.commit();  // 提交事务
        }
    }

}


